<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\api\controller;

use app\api\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\helper\Hash;
use think\Db;
use app\api\model\Menu as MenuModel;
use app\api\model\Role as RoleModel;


/**
 * @title 我的
 * @description 接口说明
 * @header name:flag require:1 default: desc:手机设备号
 */
class User extends Api
{
    /**
     * @title 我的首页
     * @description 接口说明
     * @author 开发者
     * @url /api/user/index
     * @method POST  
     * 
     * @return list:用户信息@
     * @list nickname:昵称 avatar:头像 num:Id
     * 
     * @return carousel:列表@
     * @carousel id:id image:图片 url:跳转url
     * 
     */
   public function index()
   {
        $user = $this->user;
        $list['nickname'] = $user['username'];
        $list['avatar'] = $user['avatar'];
        $list['num'] = $user['num'];
          $lb = db::name('carousel')->where('hid',$user['hid'])->field('id,image,url')->select();
          if($lb){
               foreach ($lb as $k => $v) {
                    if($v['image']){
                         $lb[$k]['image'] = get_file_path($v['image']);
                    }
               }
          }

     $data['carousel'] = $lb;
        $data['list'] = $list;
        return $this->ajaxReturn(0,'成功',$data);
   }
   /**
     * @title 更换头像
     * @description 接口说明
     * @author 开发者
     * @url /api/user/avatar
     * @method POST  
     * @param name:avatar type:int require:1 default: other: desc:头像id
     * 
     */
    public function avatar()
    {
         $user = $this->user;
         $avatar = input('avatar');
         if($avatar){
               if(db::name('user')->where('id',$user['id'])->update(['avatar'=>$avatar])){
                    return $this->ajaxReturn(0,'成功');
               }
         }else{
               return $this->ajaxReturn(1,'缺少参数');
         }
         
    }

    /**
     * @title 意见反馈
     * @description 接口说明
     * @author 开发者
     * @url /api/user/opinion
     * @method POST  
     * @param name:content type:string require:1 default: other: desc:反馈内容
     * @param name:mobile type:string require:1 default: other: desc:手机号
     * 
     */
   public function opinion()
   {
        $user = $this->user;
        $content = input('content');
        $mobile = input('mobile');
        if(!input('content')){
          return $this->ajaxReturn(1,'缺少参数');
          }
          if(!input('mobile')){
               return $this->ajaxReturn(1,'缺少参数');
          }
          $res['uid'] = $user['id'];
          $res['content'] = $content;
          $res['mobile'] = $mobile;
          $res['addtime'] = time();
          $res['hid'] = $user['hid'];
          if(db::name('opinion')->insert($res)){
               return $this->ajaxReturn(0,'感谢你的反馈意见');
          }

   }
   /**
     * @title 个人信息
     * @description 接口说明
     * @author 开发者
     * @url /api/user/get_user
     * @method POST  
     * @return list:列表@
     * @list privacy:隐私协议 protocol:用户协议
     * @return config: 1开启2关闭
     * @return role:列表@!
     * @role banquetclient:查看全部宴会客户  zeroclient:查看全部零点客户 modifybanquet:修改全部宴会客户 modifyzero:修改全部零点客户  saybanquetdata:查看全部宴会数据 sayzerodata:查看全部零点数据 saytop:查看TOP榜  excision:------------------------- retreatpersonal:零点退订个人 retreatall:零点退订全部 sayname:零点查看姓名 saycontract:零点查看合同  sayremarks:零点查看备注  saymobile:零点查看手机号 overturnall:零点翻台全部 overturnpersonal:零点翻台个人 seatall:零点入座全部 seatpersonal:零点入座个人  modifyall:零点修改全部 modifypersonal:零点修改个人 addzero:零点新增预定 notenabled:零点不启用零点  excision:------------------------- sayvisitall:营销查看全部回访  addvisit:营销添加回访  excision:------------------------- checkall:订单结单全部 checkuser:订单结单个人  excision:------------------  sayremarks:宴会查看备注 saycontract:宴会查看合同 sayname:宴会查看姓名 unsubscribeall:宴会退订全部 unsubscribepersonal:宴会退订个人  modifyall:修改全部  modifypersonal:修改个人 sayall:查看全部  saypersonal:查看个人  addscheduled:新增预定  saymobile:查看手机号
     * 
     * 
     */
   public function get_user()
   {
     $user = $this->user;
     $user = db::name('user')->where('id',$user['id'])->find();
     if($user['avatar']){
          $user['avatar'] = get_file_path($user['avatar']);
     }
     if(!$user['hotel_mobile']){
          $user['hotel_mobile'] = '0471-4166176';
          $user['hotel_addr'] = '呼和浩特如意开发区满洲里南路东岸国际东区。';

     }
     $config = db::name('config')->where('hid',$user['hid'])->where('type',1)->value('status');
     $data['config'] = $config; 

     $menu = db::name('admin_menu')
     ->where('type',1)->where('pid','neq',0)
     ->where('url_value','neq',' ')->column('id,url_value');
     
     $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
     $pids = 0;
     foreach ($pid as $vs) {
          if($vs['pid'] == 0){
               $pids = 1;
               break;
          }else{
               $pids = 2;
          }
     }
    
     $newArr = [];
    
     if($pids == 1){
          foreach ($menu as $mk => $mv) {
               $menus[$mv] = $mv;
               if($mv == 'notenabled'){
                    $menus[$mv] = 0;

               }else{
                    $menus[$mv] = 1;

               }
           }
     }elseif($pids == 2){
          foreach ($menu as $mk => $mv) {
               $menus[$mv] = $mv;
               $menus[$mv] = $mk;
           }
          $res = RoleModel::roleAuth($user['role']);
          $newArrs = [];
          if($res){
               foreach ($res as $k => $v) {
                    $str1 =str_replace("[","",$res[$k]);
                    $str1 =str_replace("]","",$str1);
                    $newArr = explode(',',$str1);
                    $newArrs = array_merge($newArr,$newArrs);
               } 
               if($newArrs){
                    foreach ($newArrs as $kn => $vn) {
                         $vn =str_replace('"',"",$vn);
                         $newArrs[$kn] = $vn;
                     }
                     foreach ($menus as $mk => $mv) {
                          foreach ($newArrs as $sk => $sv) {
                              if((int)$sv == (int)$mv){
                                   $menus[$mk] = 1;
                              }
                          }
                     }
                     foreach ($menus as $mk => $mv) {
                         if($mv != 1){
                              $menus[$mk] = 0;
                         }
                    }
               }
          }
     }
     // $user['protocol'] = 'www.baidu.com/protocol.html';
     // $user['privacy'] = 'www.baidu.com/privacy.html';
     $data['role'] = $menus;
     $data['list'] = $user;
     return $this->ajaxReturn(0,'成功',$data);
   }
    /**
     * @title 设置中心详情
     * @description 接口说明
     * @author 开发者
     * @url /api/user/set
     * @method POST  
     *  @param name:id type:int require:1 default: other: desc:查看详情id
     * 
     * @return list:列表@
     * @list id:内容id content:内容
     */
    public function set()
    {
         $user = $this->user;
         if(!input('id')){
               return $this->ajaxReturn(1,'缺少参数');
         }
         $id = input('id');
         $where[] = ['hid','=',$user['hid']];
         $where[] = ['is_hidden','=',1];
         $where[] = ['id','=',$id];
          $list = db::name('set')->where($where)->value('content');
         $data['list'] = $list;
         return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 设置中心
     * @description 接口说明
     * @author 开发者
     * @url /api/user/viewset
     * @method POST  
     * 
     * @return list:列表@
     * @list title:标题 lists:列表@
     * @lists id:查看详情 title:标题
     */
     public function viewset()
     {
          $user = $this->user;
          $search = input('search');
          $wheres[] = ['title','like',"%".$search."%"];
         
          $where[] = ['hid','=',$user['hid']];
          $where[] = ['type','=',1];
          $where[] = ['is_hidden','=',1];
          $where[] = ['pid','=',0];
          $list = db::name('set')->where($where)->field('id,title')->select();
          if($list){
               foreach ($list as $k => $v) {
                    $list[$k]['lists'] = db::name('set')->where('pid',$v['id'])->where($wheres)->where('is_hidden',1)->field('id,title')->select();
               }
          }
          $data['list'] = $list;
          return $this->ajaxReturn(0,'成功',$data);
     }
     /**
     * @title 修改密码
     * @description 接口说明
     * @author 开发者
     * @url /api/user/setpwd
     * @method POST  
     * @param name:old_password type:string require:1 default: other: desc:原密码
     * @param name:new_password type:string require:1 default: other: desc:新密码
     * @param name:again_password type:string require:1 default: other: desc:再次输入新密码
     * 
     */
    public function setpwd()
    {
         $user = $this->user;
          if(!input('old_password')){
              return $this->ajaxReturn(1,'缺少参数');
          }
          if(!input('new_password')){
               return $this->ajaxReturn(1,'缺少参数');
          }
          if(!input('again_password')){
               return $this->ajaxReturn(1,'缺少参数');
          }
         
         $old_password = input('old_password');
         $new_password = input('new_password');
         $again_password = input('again_password');
     //     print_r($old_password);exit;
         $new_password = $this->setPasswordAttr($new_password);
          
          if(!Hash::check((string)$again_password, $new_password)){
               return $this->ajaxReturn(1,'两次密码不同');
          }
          $password =  db::name('user')->where('id',$user['id'])->value('password');
          if (!Hash::check((string)$old_password, $password)) {
               return $this->ajaxReturn(1,'原密码错误');
          }    
          $update['password'] = $new_password;
          db::name('user')->where('id',$user['id'])->update($update);
          return $this->ajaxReturn(0,'成功');
    }
     // 对密码进行加密
     public function setPasswordAttr($value)
     {
          return Hash::make((string)$value);
     }
}